﻿/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF SCRUM DASHBOARD.

SCRUM DASHBOARD IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUM DASHBOARD IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUM DASHBOARD. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using ScrumDashboard.Library;
using ScrumDashboard.Library.Entities;
using ScrumDashboard.Library.Tfs;

namespace ScrumDashboard.ui
{
    public partial class AssignTo : System.Web.UI.UserControl
    {
        private string _teamName;
        private string _projectName;

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(typeof(Button), "assignToClientBTNID", "var assignToClientBTNID = '" + showAssignToUC_BTN.ClientID + "';", true);
            Page.ClientScript.RegisterStartupScript(typeof(AssignTo), "assignToClientID", "var assignToClientID = '" + assignToPopupBehavior.ClientID + "';", true);

            _teamName = ((_Default)Page).CurrentTeam;
            _projectName = ((_Default)Page).CurrentProject;
            teamMemberATTB.ActiveTabIndex = 0;
        }

        private void EnsureProjectAndTeam()
        {
            DBHelper.SaveProject(_projectName);

            if (!String.IsNullOrEmpty(_teamName))
            {
                DBHelper.SaveTeam(_projectName, _teamName);
            }
        }

        protected void Update(object sender, EventArgs e)
        {
            TfsManager tfs = new TfsManager();

            UpdateContributorsTab();

            if (_teamName != "")
            {
                Page.ClientScript.RegisterStartupScript(typeof(String), "teamName", "var teamName = '" + _teamName + "';", true);

                UpdateTeamMemberTab();
                UpdateTeamMemberEditTab();

                teamMemberTB.Enabled = true;
                teamMemberEditTB.Enabled = true;
                contibutorsTabTB.Enabled = true;

                teamMemberATTB.ActiveTab = teamMemberTB;
            }
            else
            {
                teamMemberTB.Enabled = false;
                teamMemberEditTB.Enabled = false;
                contibutorsTabTB.Enabled = true;

                teamMemberATTB.ActiveTab = contibutorsTabTB;
            }
        }

        protected void UpdateTeamMemberTab()
        {
            contributorTeamP.Controls.Clear();

            foreach (Member d in DBHelper.GetTeamMembers(_projectName, _teamName))
            {
                HyperLink contributorHL = new HyperLink();
                HtmlGenericControl aWrapperHGC = new HtmlGenericControl("div");
                aWrapperHGC.Attributes.Add("class", "nameItem");
                contributorHL.NavigateUrl = "javascript:db.assignToUser('" + d.FullName + "');";
                contributorHL.Text = d.FullName;

                aWrapperHGC.Controls.Add(contributorHL);
                contributorTeamP.Controls.Add(aWrapperHGC);
            }

            if (contributorTeamP.Controls.Count == 0)
            {
                Label noMembers = new Label();
                noMembers.Text = "No members in "+_teamName;
                contributorTeamP.Controls.Add(noMembers);
            }
        }

        protected void UpdateTeamMemberEditTab()
        {
            TfsManager tfs = new TfsManager();
            IList<string> contributors = tfs.GetProjectContributors(((_Default)Page).CurrentProject);

            teamMemberPickFrom.Items.Clear();
            teamMemberPickFrom.ClearSelection();
            foreach (string contributor in contributors)
            {
                ListItem teamMember = new ListItem();
                teamMember.Text = contributor;
                teamMember.Value = contributor;
                teamMemberPickFrom.Items.Add(teamMember);
            }

            teamMemberSelected.Items.Clear();
            teamMemberSelected.ClearSelection();
            foreach (Member d in DBHelper.GetTeamMembers(_projectName, _teamName))
            {
                ListItem teamMember = new ListItem();
                teamMember.Text = d.FullName;
                teamMember.Value = d.FullName;
                teamMemberSelected.Items.Add(teamMember);
            }
        }

        protected void UpdateContributorsTab()
        {
            TfsManager tfs = new TfsManager();
            IList<string> contributorsPersonal = tfs.GetProjectContributors(((_Default)Page).CurrentProject);

            foreach (string contributor in contributorsPersonal)
            {
                HyperLink contributorHL = new HyperLink();
                HtmlGenericControl aWrapperHGC = new HtmlGenericControl("div");
                aWrapperHGC.Attributes.Add("class", "nameItem");
                contributorHL.NavigateUrl = "javascript:db.assignToUser('" + contributor + "');";
                contributorHL.Text = contributor;
                aWrapperHGC.Controls.Add(contributorHL);
                contributorPersonalP.Controls.Add(aWrapperHGC);
            }
        }

        protected void AddTeamMember (object sender, EventArgs e)
        {
            EnsureProjectAndTeam();

            int count = teamMemberPickFrom.Items.Count;

            for(int i=0; i < count; i++)
            {
                if (teamMemberPickFrom.Items[i].Selected)
                {
                    teamMemberSelected.Items.Add(teamMemberPickFrom.Items[i]);
                    DBHelper.SaveTeamMember(_projectName, _teamName, teamMemberPickFrom.Items[i].Value);
                }
            }

            for (int i = 0; i < count; i++)
            {
                if (teamMemberPickFrom.Items[i].Selected)
                {
                    teamMemberPickFrom.Items.Remove(teamMemberPickFrom.Items[i]);
                    i--;
                    count--;
                }
            }
            teamMemberPickFrom.ClearSelection();
            teamMemberSelected.ClearSelection();
            UpdateTeamMemberTab();
            UpdateContributorsTab();
        }

        protected void RemoveTeamMember(object sender, EventArgs e)
        {
            EnsureProjectAndTeam();

            int count = teamMemberSelected.Items.Count;

            for (int i = 0; i < count; i++)
            {
                if (teamMemberSelected.Items[i].Selected)
                {
                    teamMemberPickFrom.Items.Add(teamMemberSelected.Items[i]);
                }
            }

            for (int i = 0; i < count; i++)
            {
                if (teamMemberSelected.Items[i].Selected)
                {
                    DBHelper.DeleteTeamMember(_projectName, _teamName, teamMemberSelected.Items[i].Value);
                    teamMemberSelected.Items.Remove(teamMemberSelected.Items[i]);
                    i--;
                    count--;
                }
            }
            teamMemberPickFrom.ClearSelection();
            teamMemberSelected.ClearSelection();
            UpdateTeamMemberTab();
            UpdateContributorsTab();
        }
    }
}